package com.zivwu.library.widget.banner;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.zivwu.library.R;

import net.lucode.hackware.magicindicator.MagicIndicator;

public class Banner extends FrameLayout {

    private ViewPager pager;
    private MagicIndicator indicator;
    private Runnable action;
    private BannerAdapter adapter;
    private int rotateTime = 3000;
    private boolean isRotate = false;

    public Banner(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public Banner(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    void init(@NonNull Context context, @Nullable AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.ziv_banner, this, true);
        pager = (ViewPager) this.getChildAt(0);
        indicator = (MagicIndicator) this.getChildAt(1);
        action = new Runnable() {
            @Override
            public void run() {
                pager.setCurrentItem(pager.getCurrentItem() + 1);
                postDelayed(this, rotateTime);
            }
        };
        pager.addOnPageChangeListener(new BannerPageChangeListener());
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isRotate){
            startAutoPlay();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        removeCallbacks(action);
    }

    public void startAutoPlay() {
        if (isOnly())
            return;
        isRotate = true;
        postAction();
        pager.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        removeCallbacks(Banner.this.action);
                        break;
                    case MotionEvent.ACTION_UP:
                        postAction();
                        break;
                }
                return false;
            }
        });
    }


    private void postAction() {
        removeCallbacks(action);
        postDelayed(action, rotateTime);
    }

    public void setAdapter(BannerAdapter adapter) {
        this.adapter = adapter;
        pager.setAdapter(adapter);
        indicator.setNavigator(adapter.getNavigator());
        pager.setCurrentItem(adapter.getFistPosition());
    }


    private boolean isOnly() {
        return adapter == null;
    }


    public ViewPager getPager() {
        return pager;
    }

    public MagicIndicator getIndicator() {
        return indicator;
    }


    private class BannerPageChangeListener implements ViewPager.OnPageChangeListener {


        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (indicator != null) {
                int realPosition = adapter.getRealPosition(position);
                indicator.onPageScrolled(realPosition, positionOffset, positionOffsetPixels);
            }

        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (indicator != null) {
                indicator.onPageScrollStateChanged(state);
            }
        }
    }

}
